package com.cnsuning.jobtool.jobtools.tablecount;

import com.cnsuning.jobtool.utils.ExcelUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class CaculateTableNum {
    private String targetFilename = "src/main/java/com/cnsuning/jobtool/jobtools/tablecount/files/所有表数量.txt";
    private String configFileName = "src/main/java/com/cnsuning/jobtool/jobtools/tablecount/files/所有表数量config.xlsx";
    private String allTableName = "src/main/java/com/cnsuning/jobtool/jobtools/tablecount/files/alltablename.txt";

    @Test
    public void caculate() throws IOException, InvalidFormatException {
        //需要统计的所有的表名
        List<String> allTableNameList = FileUtils.readLines(new File(this.allTableName));
        if (CollectionUtils.isEmpty(allTableNameList))
        {
            System.out.println("无需要统计的表");
            return;
        }
        Map<String, List<String>> sheetClomnsMap = new HashMap<>();
        List<String> clomns = Arrays.asList("表名", "数量");
//        sheetClomnsMap.put("omsd1", clomns);
//        sheetClomnsMap.put("omsd2", clomns);
//        sheetClomnsMap.put("omsd3", clomns);
//        sheetClomnsMap.put("omsd4", clomns);
//        sheetClomnsMap.put("omsd5", clomns);
//        sheetClomnsMap.put("omsd6", clomns);
//        sheetClomnsMap.put("omsd7", clomns);
//        sheetClomnsMap.put("omsd8", clomns);
//        sheetClomnsMap.put("omsd9", clomns);
//        sheetClomnsMap.put("omsd10", clomns);
//        sheetClomnsMap.put("omsd11", clomns);
//        sheetClomnsMap.put("omsd12", clomns);
//        sheetClomnsMap.put("omsd13", clomns);
//        sheetClomnsMap.put("omsd14", clomns);
//        sheetClomnsMap.put("omsd15", clomns);
//        sheetClomnsMap.put("omsd16", clomns);
//        sheetClomnsMap.put("omsd17", clomns);
//        sheetClomnsMap.put("omsd18", clomns);
//        sheetClomnsMap.put("omsd19", clomns);
//        sheetClomnsMap.put("omsd20", clomns);
//        sheetClomnsMap.put("omsd21", clomns);
//        sheetClomnsMap.put("omsd22", clomns);
//        sheetClomnsMap.put("omsddb23", clomns);
//        sheetClomnsMap.put("omsddb24", clomns);
//        sheetClomnsMap.put("omsddb25", clomns);
//        sheetClomnsMap.put("omsddb26", clomns);
//        sheetClomnsMap.put("omsddb27", clomns);
//        sheetClomnsMap.put("omsddb28", clomns);
//        sheetClomnsMap.put("omsddb29", clomns);
//        sheetClomnsMap.put("omsddb30", clomns);
        sheetClomnsMap.put("omsddb31", clomns);
        sheetClomnsMap.put("omsddb32", clomns);
        sheetClomnsMap.put("omsddb33", clomns);
        sheetClomnsMap.put("omsddb34", clomns);
        sheetClomnsMap.put("omsddb35", clomns);
        sheetClomnsMap.put("omsddb36", clomns);
        sheetClomnsMap.put("omsddb37", clomns);
        sheetClomnsMap.put("omsddb38", clomns);

        Map<String, List<LinkedHashMap<String, Object>>> result = ExcelUtils.readExcelHasTitle(new File(configFileName), sheetClomnsMap);
        System.out.println(result);
        //将两个结果合并
        HashMap<String,Long> tableCountHashmap = new HashMap<>();
        //遍历
        for (Map.Entry<String, List<LinkedHashMap<String, Object>>> entry: result.entrySet())
        {
            List<LinkedHashMap<String, Object>> tablecountList = entry.getValue();
            for (LinkedHashMap<String, Object> hashMap : tablecountList)
            {
                String tableName = (String) hashMap.get("表名");
                Long tableCount = (long)((double)hashMap.get("数量"));
                if (!allTableNameList.contains(tableName))
                    continue;
                Long loadedCount = tableCountHashmap.get(tableName);
                if (loadedCount == null)
                {
                    loadedCount = 0L;
                }
                tableCountHashmap.put(tableName,tableCount+loadedCount);
            }
        }
        //筛选出没有记录的表，添加记录值为0
        for (String tableName:allTableNameList)
        {
            if (!tableCountHashmap.containsKey(tableName))
            {
                tableCountHashmap.put(tableName,0L);
            }
        }

        //输出至文件
        File targetFile = new File(targetFilename);
        if (targetFile.exists()) targetFile.delete();
        for (Map.Entry<String,Long> entry : tableCountHashmap.entrySet())
        {
            FileUtils.writeStringToFile(targetFile,entry.getKey()+"|"+entry.getValue()+"\n","utf-8",true);
        }

    }
}
